package com.ljb.android.component.search.presenter

import com.ljb.android.comm.presenter.presenterScope
import com.ljb.android.comm.utils.XLog
import com.ljb.android.component.search.contract.SearchContract
import com.ljb.android.component.search.model.SearchModel
import kotlinx.coroutines.launch
import mvp.ljb.kt.presenter.BaseMvpPresenter

/**
 * @Author Kotlin MVP Plugin
 * @Date 2021/05/30
 * @Description input description
 **/
class SearchPresenter : BaseMvpPresenter<SearchContract.IView, SearchContract.IModel>(),
    SearchContract.IPresenter {

    override fun registerModel() = SearchModel::class.java

    override fun doSearch(page: Int, text: String) {
        presenterScope.launch {
            try {
                if (page == 0) {
                    getMvpView().showLoading()
                }
                getMvpView().onSearchResult(
                    getModel().doSearch(page, text)
                )
            } catch (e: Exception) {
                XLog.e(e)
            } finally {
                if (page == 0) {
                    getMvpView().hideLoading()
                }
            }
        }
    }
}
